import axios from "axios";
// import qs from 'querystring'
// import environType from "../environment/";
import { Toast } from "vant";

// let host = {
//   dev: "http://localhost:3000", // http://sctest-ylyc-app.cheoo.com/
//   pre: "http://yilu-api.prepub.cheoo.com",
//   pro: "https://api1.yilu.cn",
// };

// axios.defaults.baseURL = `${host[environType]}/`;

axios.defaults.baseURL = "http://172.30.4.193:3000/api/";


axios.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded";

  // localStorage.getItem('token')


axios.interceptors.request.use(
  (config) => {
    let token = window.localStorage.getItem("token")
    console.log("token="+token);
    if (token) {
        //将token放到请求头发送给服务器,将tokenkey放在请求头中
        config.headers.token = token;     
        //也可以这种写法
        // config.headers['accessToken'] = Token;
         return config;
    }

    Toast.loading({
      mask: false,
      duration: 0, // 一直存在
      forbidClick: true, // 禁止点击
      message: "加载中...",
    });
    return config;
  },
  () => {
    // 请求错误
    Toast.clear();
    Toast("请求错误，请求稍后重试");
  }
);

axios.interceptors.response.use(
  (res) => {
    // 请求成功
    Toast.clear();
    return res.data;
  },
  () => {
    Toast.clear();
    Toast("请求错误，请求稍后重试");
  }
);

// post请求
export function post(url: string, params: Object) {
  return new Promise((resolve, reject) => {
    axios
      .post(url, params)
      .then(
        (res) => {
          if(res.code ==1){
            resolve(res);
          }else{
            Toast.clear();
            Toast(res.msg);
          }
        },
        (err) => {
          reject(err.data);
        }
      )
      .catch((err) => {
        reject(err.data);
      });
  });
}
// get请求
export function get(url: string, params: Object) {
  return new Promise((resolve, reject) => {
    axios
      .get(url, {
        params: params,
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err.data);
      });
  });
}
